package com.caiyouxi.pay.service;

import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.common.Constant;
import com.caiyouxi.finance.model.FinanceTradeReceivable;
import com.caiyouxi.finance.model.FinanceTradeReceived;
import com.caiyouxi.finance.service.FinancePaymentDetailService;
import com.caiyouxi.finance.service.FinanceTradeReceivedService;
import com.caiyouxi.goods.model.GoodsDiscount;
import com.caiyouxi.goods.model.GoodsInfo;
import com.caiyouxi.goods.service.GoodsDiscountsService;
import com.caiyouxi.order.model.OrderBean;
import com.caiyouxi.restrict.service.RestrictedConditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class GoodsDiscountPayService{
    @Autowired
    private RestrictedConditionService restrictedConditionService;
    @Autowired
    private FinanceTradeReceivedService financeTradeReceivedService;
    @Autowired
    private GoodsDiscountsService goodsDiscountsService;
    @Autowired
    private FinancePaymentDetailService financePaymentDetailService;


    /**
     * 折扣优惠
     * flag   true生成对应流水记录false仅计算
     */
    public Integer check_goods_discount(GoodsInfo gInfo, Integer totalAmount, OrderBean bean, FinanceTradeReceivable ftr,boolean flag){
        //获取商品折扣信息（有效期内）
        GoodsDiscount goodsDiscount = goodsDiscountsService.get_goods_discounts(gInfo);//对应商品类型、商品编号查询不到，会查商品品类类型及品类编号
        if(goodsDiscount != null){
            if(check_discount(goodsDiscount,gInfo,bean.getNum(),bean.getBuyer_user_id())){
                //享受折扣
                //计算折扣后的金额，登记折扣痕迹，在finance_payment_detail表中
                totalAmount = totalAmount * goodsDiscount.getDiscount() / 100;//折扣后需支付
                if(flag){
                    //创建已收已付记录
                    FinanceTradeReceived ftrd = financeTradeReceivedService.add_finance_trade_received(totalAmount * goodsDiscount.getDiscount() / 100,ftr.getFinance_trade_details_id(),ftr.getId(), Constant.FINANCE_STATUS_CREATED);

                    //登记流水,状态已支付
                    financePaymentDetailService.add_finance_payment_detail(totalAmount - totalAmount * goodsDiscount.getDiscount() / 100,
                            ftrd.getId(),bean.getBuyer_user_id(),goodsDiscount.getId().toString(),"GOODS_DISCOUNT");
                }
            }
        }
        return totalAmount;
    }

    /**
     * 校验折扣
     */
    public boolean check_discount(GoodsDiscount goodsDiscount,GoodsInfo gInfo,Integer num,Integer user_id){
        //获取购买限制信息
        JSONObject jsb = JSONObject.parseObject(goodsDiscount.getPurchase_limited());
        String max_per_goods = jsb.getString("max_per_goods");//商品最大购买份数
        String max_order = jsb.getString("max_order");
        String duration = jsb.getString("duration");

        //先判断订单
        boolean check_order_discount = goodsDiscountsService.check_order_discount_num(max_order,duration,goodsDiscount.getId());

        //再判断商品
        boolean check_goods_discount = goodsDiscountsService.check_goods_discount_num(max_per_goods,num);

        //判断折扣业务限制条件
        boolean check_restrict = restrictedConditionService.check_restrict("GOODS_DISCOUNTS",goodsDiscount.getId().toString(),"",user_id,gInfo);

        return check_order_discount && check_goods_discount && check_restrict;
    }
}
